import time
import unittest
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from ddt import ddt,file_data
from Mysql import Mysql

@ddt()
class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.database = Mysql(host='10.59.9.12',user='root',password='yidong666',database='a3mall')
        self.driver = webdriver.Chrome()
        self.driver.get('http://10.59.9.12:7001/')
    def tearDown(self):
        time.sleep(3)
        self.driver.quit()
    @file_data('aa.yaml')
    def test_a3mall_001_login(self,username,password):
        # 点击登录-显示等待
        dl = WebDriverWait(self.driver,20,0.5).until(EC.presence_of_element_located((By.LINK_TEXT,'登录')))
        dl.click()
        # 输入用户名-隐式等待
        self.driver.find_element(By.NAME,'username').send_keys(username)
        self.driver.implicitly_wait(10)
        # 输入密码
        self.driver.find_element(By.NAME,'password').send_keys(password)
        # 强制等待
        time.sleep(7)
        # 点击登录
        self.driver.find_element(By.CLASS_NAME,'finish-btn').click()
        time.sleep(5)
        # 断言积分
        dyjf1 = self.driver.find_element(By.CLASS_NAME,'color-orage').text
        dyjf11 = dyjf1.replace(' 积分','')
        print(dyjf11)
        select = self.database.select('select point from mall_users where username = "123455556666"')
        print(select[0][0])
        assert int(dyjf11) == int(select[0][0])





    # def test_a3mall_001_case(self):
    #     self.test_a3mall_001_login()
    #     time.sleep(5)
    #     # 点击首页  /html/body/div[3]/div[2]/ul/li[1]/a
    #     self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/ul/li[1]/a').click()
    #     # 输入商品名称
    #     spname = self.driver.find_element(By.XPATH,'//input[@class="search-input" and @name="keywords"]')
    #     spname.send_keys('音乐耳机')
    #     # 回车
    #     spname.send_keys(Keys.ENTER)
    #     # 断言搜索结果
    #     dy2 = self.driver.find_element(By.XPATH,'/html/body/div[4]/a[2]').text
    #     print(dy2)
    #     # 点击商品
    #     self.driver.find_element(By.CLASS_NAME,'lazy').click()
    #     # 断言搜索结果
    #     dy3 = self.driver.find_element(By.XPATH,'/html/body/div[4]/a[3]').text
    #     print(dy3)
    #     # 提取单价
    #     dj = self.driver.find_element(By.ID,'sell-price-box').text
    #     print(dj)
    #     # 修改商品数量
    #     sl = self.driver.find_element(By.ID,'store_nums')
    #     sl.clear()
    #     time.sleep(2)
    #     sl.send_keys('2')
    #     # 点击立即购买
    #     self.driver.find_element(By.ID,'buy-cart').click()
    #     # 断言确认订单页面
    #     dy4 = self.driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
    #     print(dy4)
    #     # 选择收货地址
    #     self.driver.find_element(By.ID,'address-831').click()
    #     # 选择支付方式
    #     self.driver.find_element(By.CLASS_NAME,'radio-select-btn').click()
    #     # 提取商品数量
    #     dy5 = self.driver.find_element(By.XPATH,'//*[@id="theForm"]/div[4]/div[1]/table/tbody/tr[2]/td[3]').text
    #     print('商品数量:',dy5)
    #     # 提取商品运费
    #     dy6 = self.driver.find_element(By.ID,'real-freight').text
    #     dy61 = dy6.replace('+','')
    #     print('商品数量:',dy61)
    #     # 提取商品总价格 order-amount
    #     dy7 = self.driver.find_element(By.ID,'order-amount').text
    #     print('商品数量:',dy7)
    #     # 点击下一步
    #     self.driver.find_element(By.ID,'order-button').click()
    #     # 断言订单完成页面
    #     dy8 = self.driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
    #     print(dy8)


if __name__ == '__main__':
    unittest.main()
